package com.elf.zero.step;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.Sensor;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Binder;
import android.os.IBinder;

import androidx.annotation.Nullable;

import com.elf.zero.utils.LogUtils;

public class StepService extends Service {

    private final static String TAG = StepService.class.getSimpleName();

    public final static int notification_id = 123456;
    public final static String action_step_update = "action_step_update";

    private StepManger stepManger;
    private BroadcastReceiver broadcastReceiver;

    private final IBinder binder = new StepServiceBinder();

    public class StepServiceBinder extends Binder {

        public StepService getService() {
            return StepService.this;
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        LogUtils.i(TAG, "onBind");
        return binder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        LogUtils.i(TAG, "onCreate");

        stepManger = new StepManger(this);
        stepManger.setListener(value -> {
            stepManger.updateStep(value);
            stepManger.showNotification();
        });

        //注册广播
        registerBroadcastReceiver();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        LogUtils.i(TAG, "onStartCommand");
        startForeground(notification_id, stepManger.showNotification());
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        LogUtils.i(TAG, "onDestroy");
        stopForeground(true);

        stepManger.updateStep();
        stepManger.destroy();

        try {
            if (broadcastReceiver != null) {
                unregisterReceiver(broadcastReceiver);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onDestroy();
    }

    /**
     * 注册广播
     */
    private void registerBroadcastReceiver() {
        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                LogUtils.i(TAG, "action = " + intent.getAction());
                stepManger.updateStep();
            }
        };
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_DATE_CHANGED);
        filter.addAction(Intent.ACTION_TIME_CHANGED);
        filter.addAction(Intent.ACTION_TIME_TICK);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_SHUTDOWN);
        filter.addAction(Intent.ACTION_USER_PRESENT);
        registerReceiver(broadcastReceiver, filter);
    }

    public static void startService(Context context) {
        context.startService(new Intent(context, StepService.class));
    }

    public static void stopService(Context context) {
        context.stopService(new Intent(context, StepService.class));
    }
}
